﻿using System.Web.Mvc;
using YuYuKiCms.Dal.Interface;
using YuYuKiCms.Dal.Model;
using YuYuKiCms.Framework.DI;

namespace YuYuKiCms.Bl.Attribute
{
    public class DynamicLayoutAttribute : ActionFilterAttribute
    {
        private readonly IThemeRepository _themeRepository;
        private Theme _theme;

        public DynamicLayoutAttribute()
        {
            _themeRepository = ServiceLocator.Current.GetInstance<IThemeRepository>();
        }

        #region Overrides of ActionFilterAttribute

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            var result = filterContext.Result as ViewResult;
            if (result != null)
            {
                if (_theme == null)
                {
                    _theme = _themeRepository.GetDefaultTheme();
                    if (_theme != null)
                    {
                        result.MasterName = _theme.PathView;
                    }
                }
            }
        }

        #endregion
    }
}